/*
 * (C) Copyright 2014 Kurento (http://kurento.org/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.one2manycall.vo;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.kurento.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.google.gson.JsonObject;

/**
 * User session.
 *
 * @author Boni Garcia (bgarcia@gsyc.es)
 * @author Micael Gallego (micael.gallego@gmail.com)
 * @since 4.3.1
 */

@Data
//@NoArgsConstructor
public class UserSession  implements Serializable {



    private static final Logger log = LoggerFactory.getLogger(UserSession.class);

    /**
     * 名称
     */
    private  String userId = "";


    private int state;
    /**
     * 名称
     */
    private  String name = "";
    /**
     * 对话的session websocket
     */
    private final WebSocketSession session ;

    private String sdpOffer;
    /**
     * 呼叫目标
     */
    private String callingTo;
    /**
     * 呼叫来源
     */
    private String callingFrom;


    private String stateCalling;

    private String stateFree;

    /**
     * 通讯连接点
     */
    private WebRtcEndpoint webRtcEndpoint;



    private RecorderEndpoint recorderEndpoint;


    private MediaPipeline mediaPipeline;


    private WebRtcEndpoint playingWebRtcEndpoint;

    private PlayerEndpoint playerEndpoint;

    private final List<IceCandidate> candidateList = new ArrayList<IceCandidate>();

    public UserSession(WebSocketSession session, String name) {
        this.session = session;
        this.name = name;
    }

    public WebSocketSession getSession() {
        return session;
    }

    public String getName() {
        return name;
    }

    public String getSdpOffer() {
        return sdpOffer;
    }

    public void setSdpOffer(String sdpOffer) {
        this.sdpOffer = sdpOffer;
    }

    public String getCallingTo() {
        return callingTo;
    }

    public void setCallingTo(String callingTo) {
        this.callingTo = callingTo;
    }

    public String getCallingFrom() {
        return callingFrom;
    }

    public void setCallingFrom(String callingFrom) {
        this.callingFrom = callingFrom;
    }

    public void sendMessage(JsonObject message) throws IOException {
        log.debug("Sending message from user '{}': {}", name, message);
        session.sendMessage(new TextMessage(message.toString()));
    }


    public void sendChatDataVo(ChatDataVo chatDataVo) throws IOException {

        String s = JSON.toJSONString(chatDataVo);

        log.debug("Sending message from user : {}",  s);
        session.sendMessage(new TextMessage(s));
    }


    public String getSessionId() {
        return session.getId();
    }

    public void setWebRtcEndpoint(WebRtcEndpoint webRtcEndpoint) {
        this.webRtcEndpoint = webRtcEndpoint;

        for (IceCandidate e : candidateList) {
            this.webRtcEndpoint.addIceCandidate(e);
        }
        this.candidateList.clear();
    }

    public void addCandidate(IceCandidate candidate) {
        if (this.webRtcEndpoint != null) {
            this.webRtcEndpoint.addIceCandidate(candidate);
        } else {
            candidateList.add(candidate);
        }
    }

    public void clear() {
        this.webRtcEndpoint = null;
        this.candidateList.clear();
    }





    public UserSession(WebSocketSession session) {
        this.session = session;
    }



    public WebRtcEndpoint getWebRtcEndpoint() {
        return webRtcEndpoint;
    }

    public void release() {
        this.playerEndpoint.stop();
        this.mediaPipeline.release();
    }

}
